
import axios from '../../../axios/index'
const vtp = {
  set (key, data) {
    localStorage.setItem(key, JSON.stringify(data))
  },
  get (key) {
    return JSON.parse(localStorage.getItem(key))
  },
  rem (key) {
    localStorage.removeItem(key)
  },
  winHei (o = 0) {
    return document.documentElement.clientHeight - o
  },
  winWid (o = 0) {
    return document.documentElement.clientWidth - o
  },
  fildata (data, key, hndn = 'selNavJson', reNum) {
    var item = data
    for (var i = 0; i < data.length; i++) {
      if (item[i].path === key) {
        vtp.set(hndn, item[i])
        reNum = i
      } else {
        if (item[i].children !== undefined) { vtp.fildata(item[i].children, key) }
      }
    }
    return [vtp.get(hndn), hndn, reNum]
        // vtp.get('selNavJson')
  },
  str (o) {
    return JSON.stringify(o)
  },
  par (o) {
    return JSON.parse(o)
  },
  getAfterStr (o, s, isb = 0, ifs = 1) {
    o = ifs ? o[isb] : o
    return s ? o.substring(o.lastIndexOf('/')) : o.substring(o.lastIndexOf('/')).split('/')[1]
  },
  getBeforeStr (o) {
    return o.substring(0, o.lastIndexOf('/'))
  },
  returnRootPathStr (o) {
    let d = vtp.getBeforeStr(o)
    if (d === '') { d = o }
    return d
  },
  removeStr (o) {
    return o.split('/')[1]
  },
  toRouter(o,src){
    o.$router.push(src);
  },
  strToNum(o,size = 21){
    return o.toString().length * size;
  },
  reThis(o){
    return o.currentTarget;
  },
  reData(o,i){
    o.splice(i, 1);
  },
  timeValFun(d1){
    if(d1){//di作为一个变量传进来
        //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
        var dateBegin = new Date(d1.replace(/-/g, "/"));//将-转化为/，使用new Date
        var dateEnd = new Date();//获取当前时间
        var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
        var leave1=dateDiff%(24*3600*1000) //计算天数后剩余的毫秒数
        var hours=Math.floor(leave1/(3600*1000))//计算出小时数
        //计算相差分钟数
        var leave2=leave1%(3600*1000) //计算小时数后剩余的毫秒数
        var minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
        //计算相差秒数
        var leave3=leave2%(60*1000) //计算分钟数后剩余的毫秒数
        var seconds=Math.round(leave3/1000)
        // console.log(" 相差 "+dayDiff+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
        // console.log(dateDiff+"时间差的毫秒数",dayDiff+"计算出相差天数",leave1+"计算天数后剩余的毫秒数" ,hours+"计算出小时数",minutes+"计算相差分钟数",seconds+"计算相差秒数");
        return {'day':dayDiff,'hours':hours,'minutes':minutes,'seconds':seconds}
    }
    
  },
  addZero (num) {
    if (parseInt(num) < 10) {
      num = '0' + num
    }
    return num
  },
    dataDicVal(v,k,fun = () => {} ){
        if(v){
            if(Array.isArray(v)){
                v.map((item1,i) => {
                    axios.get(axios.dicUrl+k).then(data => { 
                        if(data){
                            let info = data.filter(item => { return item.dicItemCode == item1 ? true : false });
                            v[i] = info[0].dicItemName;
                            if(i == (v.length-1)){fun(v,data);}
                        }else{
                            fun('未匹配字段')
                        }
                    })
                });
            }else{
                axios.get(axios.dicUrl+k).then(data => { 
                    if(data){
                        let info = data.filter(item => { return item.dicItemCode == v ? true : false });fun(info[0],data);
                    }else{
                        fun('未匹配字段')
                    }
                })
            }
        }
    
    },
    winSize(fun = () => {}){
        window.onresize = () => {
            return (() => {
                fun(this.winHei());
            })()
        }
    },
    updataState(o,t,fun = ()=>{}){
        if (o && o.code === 0) {
            t.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
            })
            fun();
        } else {
            t.$message.error(o.msg)
        }
        
    },

}

export default vtp
